#ifndef _FUNCTION_HPP
#define _FUNCTION_HPP

#include <lua.hpp>
#include "Args.hpp"
#include "Module.hpp"

#define lua_registryIndex int

static inline int lua_tofunction(lua_State *L, int idx)
{
	lua_pushvalue(L, idx);
	int ref = luaL_ref(L, LUA_REGISTRYINDEX);
	lua_pop(L, 1);
	return ref;
}

namespace lua
{

class Function
{
public:
	Function(lua_registryIndex luaFunc);
	void unref();
	void operator()(lua::Args args = lua::Args(Module::currentModule));
	bool operator==(lua::Function &other);
	Module* getModule();
private:
	int mLuaRef;
	Module *mModule;
};

}

#endif